home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / BlobMgr / Demo Folder / TextDlog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  4.6 KB  |  207 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Present a dialog box with a text rectangle, a scroll bar, and an OK
  3.  * button.  This can be used as a help dialog.  Pass in a dialog
  4.  * resource number and a handle to the text.  The dialog should
  5.  * have the OK button as the first item, and user items for the
  6.  * second and third items.  The second is the rect in which the
  7.  * control is drawn, the third is the rect in which text is drawn.
  8.  * Only the OK button should be enabled.
  9.  *
  10.  * The text handle should be locked before calling TextDlog and
  11.  * unlocked after.
  12.  */
  13.  
  14. # include    "TransSkel.h"
  15.  
  16. # include    "BlobMgr.h"
  17. # include    "BlobDemo.h"
  18.  
  19.  
  20. enum        /* item numbers */
  21. {
  22.     iOK = 1,            /* OK button */
  23.     iScrollBar,            /* scroll bar user item */
  24.     iTextRect,            /* text rectangle user item */
  25.     iOutline            /* OK button user item */
  26. };
  27.  
  28.  
  29. static ControlHandle    theScroll;
  30. static Boolean            needScroll;    /* true if need scroll bar */
  31. static short    curLine;
  32. static short            halfPage;
  33. static TEHandle            teHand;
  34.  
  35.  
  36. /*
  37.  * Proc for drawing scroll bar user item.
  38.  */
  39.  
  40. static pascal void
  41. DrawScroll (DialogPtr theDialog, short itemNo)
  42. {
  43.     if (needScroll)
  44.         ShowControl (theScroll);
  45. }
  46.  
  47.  
  48. /*
  49.  * Proc for drawing text display user item.
  50.  */
  51.  
  52. static pascal void
  53. DrawTextRect (DialogPtr theDialog, short itemNo)
  54. {
  55. Rect    r;
  56.  
  57.     SkelGetDlogRect (theDialog, iTextRect, &r);
  58.     if (needScroll)
  59.         FrameRect (&r);
  60.     r = (**teHand).viewRect;
  61.     TEUpdate (&r, teHand);
  62. }
  63.  
  64.  
  65. /*
  66.  * Scroll to the correct position.  lDelta is the
  67.  * amount to CHANGE the current scroll setting by.
  68.  */
  69.  
  70. static void
  71. DoScroll (short lDelta)
  72. {
  73. short    newLine;
  74.  
  75.     newLine = curLine + lDelta;
  76.     if (newLine < 0) newLine = 0;
  77.     if (newLine > GetCtlMax (theScroll)) newLine = GetCtlMax (theScroll);
  78.     SetCtlValue (theScroll, newLine);
  79.     lDelta = (curLine - newLine ) * (**teHand).lineHeight;
  80.     TEScroll (0, lDelta, teHand);
  81.     curLine = newLine;
  82. }
  83.  
  84.  
  85. static pascal void
  86. __TrackScroll (ControlHandle theScroll, short partCode)
  87. {
  88. short    lDelta;
  89.  
  90.     if (partCode == GetCRefCon (theScroll))    /* still in same part? */
  91.     {
  92.         switch (partCode)
  93.         {
  94.             case inUpButton: lDelta = -1; break;
  95.             case inDownButton: lDelta = 1; break;
  96.             case inPageUp: lDelta = -halfPage; break;
  97.             case inPageDown: lDelta = halfPage; break;
  98.         }
  99.         DoScroll (lDelta);
  100.     }
  101. }
  102.  
  103.  
  104. /*
  105.  * Filter to handle hits in scroll bar
  106.  */
  107.  
  108. static pascal Boolean
  109. TextFilter (DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
  110. {
  111. Point    thePoint;
  112. short    thePart;
  113.  
  114.     if (theEvent->what == mouseDown)
  115.     {
  116.         thePoint = theEvent->where;
  117.         GlobalToLocal (&thePoint);
  118.         thePart = TestControl (theScroll, thePoint);
  119.         if (thePart == inThumb)
  120.         {
  121.             (void) TrackControl (theScroll, thePoint, nil);
  122.             DoScroll (GetCtlValue (theScroll) - curLine);
  123.         }
  124.         else if (thePart != 0)
  125.         {
  126.             SetCRefCon (theScroll, (long) thePart);
  127.             (void) TrackControl (theScroll, thePoint, __TrackScroll);
  128.         }
  129.     }
  130.     return (false);
  131. }
  132.  
  133.  
  134. void
  135. TextDialog (short dlogNum, Handle textHandle,
  136.             short fontNum, short fontSize, Boolean wrap)
  137. {
  138. GrafPtr        oldPort;
  139. DialogPtr    theDialog;
  140. short        itemNo;
  141. Rect        r;
  142. short        scrollLines;
  143. short        viewLines;
  144. Handle        oldHText;
  145. ModalFilterProcPtr filterProc = (ModalFilterProcPtr) nil;
  146.  
  147.     GetPort (&oldPort);
  148.     theDialog = GetNewDialog (dlogNum, nil, (WindowPtr) -1L);
  149.     if (theDialog == (DialogPtr) nil)
  150.     {
  151.         SysBeep (1);
  152.         return;
  153.     }
  154.  
  155.     SkelPositionWindow (theDialog, skelPositionOnParentDevice,
  156.                                     FixRatio (1, 2), FixRatio (1, 5));
  157.  
  158.     /* install OK button outliner */
  159.     SkelSetDlogButtonOutliner (theDialog, iOutline);
  160.  
  161.     SkelSetDlogProc (theDialog, iTextRect, DrawTextRect);
  162.  
  163.     /*
  164.      * Incorporate text into a TERec.
  165.      */
  166.     SetPort (theDialog);
  167.     TextFont (fontNum);
  168.     TextSize (fontSize);
  169.     SkelGetDlogRect (theDialog, iTextRect, &r);
  170.     InsetRect (&r, 4, 4);
  171.     teHand = TENew (&r, &r);
  172.     if (!wrap)
  173.         (**teHand).crOnly = -1;
  174.     oldHText = (**teHand).hText;        /* save this.  restore later */
  175.     (**teHand).hText = textHandle;
  176.     TECalText (teHand);
  177.     viewLines = (r.bottom - r.top) / (**teHand).lineHeight;
  178.     scrollLines = (**teHand).nLines - viewLines;
  179.     needScroll = (scrollLines > 0);
  180.     SkelSetDlogProc (theDialog, iScrollBar, DrawScroll);
  181.     SkelGetDlogRect (theDialog, iScrollBar, &r);
  182.     if (needScroll)
  183.     {
  184.         theScroll = NewControl (theDialog, &r, "\p", false, 0, 0, 0,
  185.                         scrollBarProc, 0L);
  186.         SetCtlMax (theScroll, scrollLines);
  187.         halfPage = viewLines / 2;
  188.         curLine = 0;
  189.         filterProc = TextFilter;
  190.     }
  191.  
  192.     ShowWindow (theDialog);
  193.     ModalDialog (SkelDlogFilter (filterProc, true), &itemNo);
  194.     SkelRmveDlogFilter ();
  195.  
  196.     /*
  197.      * Restore hText field of TE record before disposing of it.
  198.      */
  199.     (**teHand).hText = oldHText;
  200.     TEDispose (teHand);
  201.     /*ReleaseResource (textHandle);*/
  202.     if (needScroll)
  203.         DisposeControl (theScroll);
  204.     SetPort (oldPort);
  205.     DisposDialog(theDialog);
  206. }
  207.